package com.nexgo.payment.trans.lock;

import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.nexgo.payment.aop.annotation.RunOnMainThread;
import com.nexgo.payment.databases.DataSource;
import com.nexgo.payment.databases.PasswordInfo;
import com.nexgo.payment.util.EncryptUtils;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * 锁定终端业务操作类
 *
 * @author 谭忠扬-YuriTam
 * @time 2017年8月22日
 */
public class LockTerminalPresenter implements LockTerminalContract.Presenter {

    private final LockTerminalContract.View mView;
    private final DataSource mRepository;

    public LockTerminalPresenter(@NonNull LockTerminalContract.View view, @NonNull DataSource repository) {
        mView = checkNotNull(view);
        mRepository = checkNotNull(repository);
        mView.setPresenter(this);
    }

    @Override
    public void start() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {

    }

    @Override
    public void getInputDialog() {
        postMainThread(() -> mView.showInputDialog());
    }

    @Override
    public void login(String password) {
        if (TextUtils.isEmpty(password)) {
            postMainThread(() -> mView.showEmptyInput());
            return;
        }
        PasswordInfo info = mRepository.getPasswordInfo();
        if (!info.getSecurityPassword().equals(EncryptUtils.encryptSHA256ToString(password))) {
            postMainThread(() -> mView.showErrPassword());
            return;
        }
        postMainThread(() -> mView.showExistLock());
    }

    @RunOnMainThread
    private void postMainThread(Runnable runnable) {
        runnable.run();
    }
}
